package com.javasm.finance.controller;

import com.javasm.finance.service.impl.ProdServiceImpl;
import com.javasm.finance.service.ProdService;
import com.javasm.finance.util.CodeMsg;
import com.javasm.finance.util.ReturnRes;

import com.javasm.finance.entity.PageInfo;
import com.javasm.finance.entity.Product;
import com.javasm.finance.entity.ReturnEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/recommend/*")
public class ProdController extends BaseController {
    ProdService prodService = new ProdServiceImpl();

    public void addRelevance(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("执行MenuController addRelevance");
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String parentIdStr = req.getParameter("parentId");
        Integer parentId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)) {
            parentId = Integer.valueOf(parentIdStr);
        }
        String relevance = req.getParameter("relevance");
        boolean addRelevance = prodService.addRelevance(relevance, parentId, prodId);
        ReturnEntity re = new ReturnEntity();
        if (addRelevance) {
            re.setReturnCode(CodeMsg.OPERATE_Success.getReturnCode());
            re.setReturnMsg(CodeMsg.OPERATE_Success.getReturnMsg());
        } else {
            re.setReturnCode(CodeMsg.OPERATE_FAIL.getReturnCode());
            re.setReturnMsg(CodeMsg.OPERATE_FAIL.getReturnMsg());
        }

        ReturnRes.Return(resp, re);
        ;
    }

    public void Relevance(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        List<Integer> queryRelevanceId = prodService.findQueryRelevanceId(prodId);
        System.out.println(queryRelevanceId);
        ReturnEntity re = new ReturnEntity();
        re.setReturnCode(CodeMsg.Query_Success.getReturnCode());
        re.setReturnMsg(CodeMsg.Query_Success.getReturnMsg());
        re.setReturnData(queryRelevanceId);
        ReturnRes.Return(resp, re);
    }

    public void queryRelevance(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("执行ProdController queryRelevance方法");
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String parentIdStr = req.getParameter("parentId");
        Integer parentId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)) {
            parentId = Integer.valueOf(parentIdStr);
        }
        List<Product> queryRelevanceList = prodService.findQueryRelevance(parentId, prodId);
        System.out.println(queryRelevanceList);
        ReturnEntity re = new ReturnEntity();
        if (queryRelevanceList != null && queryRelevanceList.size() > 0) {
            re.setReturnCode(CodeMsg.Query_Success.getReturnCode());
            re.setReturnMsg(CodeMsg.Query_Success.getReturnMsg());
            Map<String, Object> map = new HashMap<>();
            map.put("queryRelevanceList", queryRelevanceList);
            re.setReturnData(map);
        } else {
            re.setReturnCode(CodeMsg.Query_Fail.getReturnCode());
            re.setReturnMsg(CodeMsg.Query_Fail.getReturnMsg());
        }
        ReturnRes.Return(resp, re);

    }


    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("执行ProdController delete方法");
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        boolean delete = prodService.delete(prodId);
        ReturnEntity re = new ReturnEntity();
        if (delete) {
            re.setReturnCode(CodeMsg.OPERATE_Success.getReturnCode());
            re.setReturnMsg(CodeMsg.OPERATE_Success.getReturnMsg());

        } else {
            re.setReturnCode(CodeMsg.OPERATE_FAIL.getReturnCode());
            re.setReturnMsg(CodeMsg.OPERATE_FAIL.getReturnMsg());
        }
        ReturnRes.Return(resp, re);
    }

    public void queryProd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("执行ProdController queryProd方法");
        List<Product> queryProd = prodService.findQueryProd();
        ReturnEntity re = new ReturnEntity();
        if (queryProd != null && queryProd.size() > 0) {
            re.setReturnCode(CodeMsg.Query_Success.getReturnCode());
            re.setReturnMsg(CodeMsg.Query_Success.getReturnMsg());
            re.setReturnData(queryProd);
        } else {
            re.setReturnCode(CodeMsg.Query_Fail.getReturnCode());
            re.setReturnMsg(CodeMsg.Query_Fail.getReturnMsg());
        }
        ReturnRes.Return(resp, re);
    }

    public void addProd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("执行ProdController addPord方法");
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String prodName = req.getParameter("prodName");
        String prodReco = req.getParameter("prodReco");
        String prodFirstStr = req.getParameter("prodFirst");
        Integer prodFirst = null;

        if (prodFirstStr != null && !"".equals(prodFirstStr)) {
            prodFirst = Integer.valueOf(prodFirstStr);
        }

        String onlineSubStr = req.getParameter("onlineSub");
        Integer onlineSub = null;
        if (onlineSubStr != null && !"".equals(onlineSubStr)) {
            onlineSub = Integer.valueOf(onlineSubStr);
        }
        String prodisvisibleStr = req.getParameter("prodisvisible");
        Integer prodisvisible = null;
        if (prodisvisibleStr != null && !"".equals(prodisvisibleStr)) {
            prodisvisible = Integer.valueOf(prodisvisibleStr);

        }
        String prodReason = req.getParameter("prodReason");
        Product parent = prodService.finQueryProdParent(prodName);
        System.out.println(parent);
        Product product = new Product(prodId, prodName, prodReco, prodFirst, onlineSub, prodisvisible, prodReason, parent.getParentClass(), parent.getParentId());
        System.out.println(product);
        int i = prodService.addProd(product);

        ReturnEntity re = new ReturnEntity();
        if (i > 0) {
            re.setReturnCode(CodeMsg.OPERATE_Success.getReturnCode());
            re.setReturnMsg(CodeMsg.OPERATE_Success.getReturnMsg());

        } else {
            re.setReturnCode(CodeMsg.OPERATE_FAIL.getReturnCode());
            re.setReturnMsg(CodeMsg.OPERATE_FAIL.getReturnMsg());
        }
        ReturnRes.Return(resp, re);

    }


    public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("执行ProdController edit方法");
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String prodName = req.getParameter("prodName");
        String prodReco = req.getParameter("prodReco");
        String prodFirstStr = req.getParameter("prodFirst");
        Integer prodFirst = null;
        if (prodFirstStr != null && !"".equals(prodFirstStr)) {
            prodFirst = Integer.valueOf(prodFirstStr);
        }
        String onlineSubStr = req.getParameter("onlineSub");
        Integer onlineSub = null;
        if (onlineSubStr != null && !"".equals(onlineSubStr)) {
            onlineSub = Integer.valueOf(onlineSubStr);
        }
        String prodisvisibleStr = req.getParameter("prodisvisible");
        Integer prodisvisible = null;
        if (prodisvisibleStr != null && !"".equals(prodisvisibleStr)) {
            prodisvisible = Integer.valueOf(prodisvisibleStr);

        }
        String prodReason = req.getParameter("prodReason");
        String relevance = req.getParameter("relevance");
        Product product = new Product(prodId, prodName, prodReco, prodFirst, onlineSub, prodisvisible, prodReason, relevance);
        int edit = prodService.edit(product);
        ReturnEntity re = new ReturnEntity();
        if (edit > 0) {
            re.setReturnCode(CodeMsg.OPERATE_Success.getReturnCode());
            re.setReturnMsg(CodeMsg.OPERATE_Success.getReturnMsg());
        } else {
            re.setReturnCode(CodeMsg.OPERATE_FAIL.getReturnCode());
            re.setReturnMsg(CodeMsg.OPERATE_FAIL.getReturnMsg());
        }
        ReturnRes.Return(resp, re);


    }

    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("执行ProdController toEdit方法");
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        Product byProdId = prodService.findByProdId(prodId);
        ReturnEntity re = new ReturnEntity();
        if (byProdId != null) {
            re.setReturnCode(CodeMsg.Query_Success.getReturnCode());
            re.setReturnMsg(CodeMsg.Query_Success.getReturnMsg());
            re.setReturnData(byProdId);
        } else {
            re.setReturnCode(CodeMsg.Query_Fail.getReturnCode());
            re.setReturnMsg(CodeMsg.Query_Fail.getReturnMsg());
        }
        ReturnRes.Return(resp, re);

    }


    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行ProdController query方法");
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String prodName = req.getParameter("prodName");
        Integer page = 1;
        String pageStr = req.getParameter("page");
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.valueOf(pageStr);
        }
        //每页5条
        Integer pageSize = 5;
        String pageSizeStr = req.getParameter("pageSize");
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.valueOf(pageSizeStr);
        }
        List<Product> prodList = prodService.findQueryAll(page, pageSize, prodName);
        ReturnEntity re = new ReturnEntity();
        if (prodList != null && prodList.size() > 0) {
            long totalNum = prodService.findTotalNum(prodName);
            PageInfo pageInfo = new PageInfo(page, pageSize, totalNum);
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("prodList", prodList);
            returnMap.put("pageInfo", pageInfo);
            re.setReturnCode(CodeMsg.Query_Success.getReturnCode());
            re.setReturnMsg(CodeMsg.Query_Success.getReturnMsg());
            re.setReturnData(returnMap);
        } else {
            Map<String, Object> hashMap = new HashMap<>();
            PageInfo pageInfo = new PageInfo(1, 5, 0l);
            hashMap.put("pageInfo", pageInfo);
            re.setReturnCode(CodeMsg.Query_Fail.getReturnCode());
            re.setReturnMsg(CodeMsg.Query_Fail.getReturnMsg());
        }
        ReturnRes.Return(resp, re);
    }

}
